#include "pointMotionTo6ko.h"

short nextStep[6] = {0};

void findNextStep(point_t* pPoint)
{
short xInd,yInd,xLaw, yLaw, pointNum;

	for(pointNum=0; pointNum<3; pointNum++)
	{
		xInd= (pPoint + pointNum)->x;
		yInd = (pPoint + pointNum)->y;
		xLaw = ((pPoint + pointNum)->move)->aplToX ;
		yLaw = ((pPoint + pointNum)->move)->aplToY ;

	
		nextStep[pointNum*2 + 0] = xInd + xLaw;	
		nextStep[pointNum*2 + 1] = yInd + yLaw;	
	}
}


//
// check for coliision between display bound and point
//

	
int CollisionCheckBound(int pointIndex)
{
	if((nextStep[pointIndex*2 + 0] > 95 || nextStep[pointIndex*2 + 0] < 0) ||
	  (nextStep[pointIndex*2 + 1]>15 || nextStep[pointIndex*2 + 1]< 0))
	   {
	  return 1;
	  }
	  else {
	  return 0;
	  }
}




//
// check for coliision between two points
//

int CollisionCheckPoint(void)
{
int i=0;
	
	if((nextStep[i*2 + 0] == nextStep[(i+1)*2 + 0] && nextStep[i*2 + 1] == nextStep[(i+1)*2 + 1])
	||  (nextStep[i*2 + 0] == nextStep[(i+2)*2 + 0] && nextStep[i*2 + 1] == nextStep[(i+2)*2 + 1])
	|| 	(nextStep[(i+1)*2 + 0] == nextStep[(i+2)*2 + 0] && nextStep[(i+1)*2 + 1] == nextStep[(i+2)*2 + 1]) )

	return 1;
	else
	return 0;
}



//
// change the point's direction  if  point touch tbe bound of display
//



//
// change the point's direction  if  point touch tbe bound of display
//



int angleCollision(int pointNum)
{
    if(  (nextStep[pointNum*2 + 0]>95 && nextStep[pointNum*2 + 1]<0 ) || 
     (nextStep[pointNum*2 + 0]>95 && nextStep[pointNum*2 + 1]> 15)  ||
     (nextStep[pointNum*2 + 0]<0 && nextStep[pointNum*2 + 1]<0 ) || 
     (nextStep[pointNum*2 + 0]>0 && nextStep[pointNum*2 + 1]> 15)  )
     return 1;
    else
        return 0;
}

//
// change the point's direction  if  point touch tbe bound of display
//



void 
changePosition (point_t* pPoint)
{
    int i;            //index of the used move law
    int pointNum=0;        // Index for the point number
     

    
nextStepCheck:
    findNextStep(pPoint);
            
        for(pointNum=0; pointNum<3; pointNum++)
        {
		i=((pPoint+pointNum)->move - moveLaws)/sizeof(moveLaws);
            if(CollisionCheckBound(pointNum))
            {

                 // touching the up and down side of display
               
                  if( (nextStep[pointNum*2 + 0]>95 || nextStep[pointNum*2 + 0]<0 ) &&
                        (nextStep[pointNum*2 + 1]<=15 && nextStep[pointNum*2 + 1]>=0) && (i>3) )
                      
                 {
                      if (i==6)                  // if point comes from left side
                      {
                       (pPoint+pointNum)->move=&moveLaws[4];
                      }
                      else
                      if (i==4)
                      {
                        (pPoint+pointNum)->move=&moveLaws[6];
                      }
                         
                      else 
                       if(i==5)
                      {
                        (pPoint+pointNum)->move=&moveLaws[7];
                      }
                      else
                        if (i==7)
                      {
                       (pPoint+pointNum)->move=&moveLaws[5];
                      }

                 }
                 // touching the legt and right side of risplay
                  else 
                 
                  if ( (nextStep[pointNum*2 + 0]<=95 && nextStep[pointNum*2 + 0]>=0 ) &&
                 (nextStep[pointNum*2 + 1]>15 || nextStep[pointNum*2 + 1]<0 ) && (i>3)  )
                {
                    if(i==6)
                        (pPoint+pointNum)->move=&moveLaws[5];
                    else
                        if(i==5)
                       (pPoint+pointNum)->move=&moveLaws[6];
                    else
                        if(i==7)
                        (pPoint+pointNum)->move=&moveLaws[4];
                    else 
                       if(i==4)
                       (pPoint+pointNum)->move=&moveLaws[7];
                     
                 }
             
                if( (nextStep[pointNum*2 + 0]>95 || nextStep[pointNum*2 + 0]<0  || 
                  nextStep[pointNum*2 + 1]>15 || nextStep[pointNum*2 + 1]<0 ) && ( i<4 || angleCollision(pointNum)) )
                 {
                  if(i  == 0 )  
                  (pPoint+pointNum)->move= &moveLaws[1];
                  else
				   if(i  == 1 )  
                  (pPoint+pointNum)->move= &moveLaws[0];
                  else
				   if(i  == 2 )  
                  (pPoint+pointNum)->move= &moveLaws[3];
                  else
				   if(i  == 3 )  
                  (pPoint+pointNum)->move= &moveLaws[2];
                  else
				       if(i  == 4 )  
                  (pPoint+pointNum)->move= &moveLaws[5];
                  else
				   if(i  == 5 )  
                  (pPoint+pointNum)->move= &moveLaws[4];
                  else
				   if(i  == 6 )  
                  (pPoint+pointNum)->move= &moveLaws[7];
                  else
				   if(i  == 7 )  
                  (pPoint+pointNum)->move= &moveLaws[6];
                }
              
            goto nextStepCheck;
              }
        
            pointWritePosition(pPoint+pointNum, nextStep[pointNum*2 + 0], nextStep[pointNum*2 + 1]);
        }

}
